package com.extension.company.view.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.contract.home.TwoCategoryContract;
import com.extension.company.presenter.home.TwoCategoryPresenter;
import com.extension.company.common.widget.EnhanceTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * create zwk
 * data   2019/2/19
 * 描述：fragmentHome中“涨技能、惠租房....”模块 activity
 */
public class TwoCategory extends BaseTitleActivity<TwoCategoryContract.View, TwoCategoryPresenter>
		implements TwoCategoryContract.View {

	@BindView(R.id.tabLayout)
	EnhanceTabLayout tabLayout;

	@BindView(R.id.viewpager)
	ViewPager viewpager;

	private final String[] sTitle = {"推荐", "冰箱", "电视机", "空调", "洗衣机", "生活小电器", "笔记本"};

	private List<Fragment> fragmentList;

	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, TwoCategory.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("精品家电");
		viewpager.setOffscreenPageLimit(1);

		fragmentList = new ArrayList<Fragment>();
		for (int i = 0; i < sTitle.length; i++) {
			tabLayout.addTab(sTitle[i]);
			TwoCategoryFragment fragment = TwoCategoryFragment.newinstance();
			if (i == 0) {
				Bundle bundle = new Bundle();
				bundle.putBoolean("isFirst", true);
				fragment.setArguments(bundle);
			}
			fragmentList.add(fragment);
		}
		viewpager.setAdapter(new MorePagerAdapter(getSupportFragmentManager()));
		viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
		tabLayout.setupWithViewPager(viewpager);
	}

	@Override
	protected void initListener() {

	}

	@Override
	public int getMainLayout() {
		return R.layout.home_twocategory;
	}

	@Override
	public TwoCategoryContract.View createView() {
		return this;
	}

	@Override
	public TwoCategoryPresenter createPresenter() {
		return new TwoCategoryPresenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	private class MorePagerAdapter extends FragmentPagerAdapter {

		public MorePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			return fragmentList.get(i);
		}

		@Override
		public int getCount() {
			return fragmentList.size();
		}

		@Override
		public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//            super.destroyItem(container, position, object);
		}
	}

}
